package jwlee;

import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.*;
import java.awt.*;

public class ScreenCamera {

	private String fileName = "ScreenCamera"; // 文件的前缀

	private String imageFormat = "png"; // 图像文件的格式

	static int serialNum = 0;

	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

	public ScreenCamera() {
	}

	public ScreenCamera(String s, String format) {
		fileName = s;
		imageFormat = format;
	}

	/***************************************************************************
	 * 对屏幕进行拍照 snapShot the Gui once
	 **************************************************************************/
	public void snapShot() {
		try {
			// 拷贝屏幕到一个BufferedImage对象screenshot
			Rectangle screenRect = new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight());
			BufferedImage screenshot = new Robot().createScreenCapture(screenRect);

			serialNum++;
			// 根据文件前缀变量和文件格式变量，自动生成文件名
			String name = fileName + String.valueOf(serialNum) + "." + imageFormat;
			File f = new File(name);
			System.out.println("Save File:" + name);

			// 将screenshot对象写入图像文件
			ImageIO.write(screenshot, imageFormat, f);

			System.out.println("Finished!");
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}
	
	public static void draw(){
		BufferedImage img = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
		Color c = new Color(199,237,204);
		//img.getGraphics().drawImage(img, 100, 100, c, null);
		img.getGraphics().setColor(c);
		img.createGraphics().setColor(c);
		img.getGraphics().fillRect(100, 100, 800, 600);
		//img.createGraphics().fillRect(0, 0, 800, 600);
		img.getGraphics().drawLine(0, 0, 99, 199); 
		File f = new File("c:/1.jpg");
		try {
			ImageIO.write(img, "jpg", f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		ScreenCamera cam = new ScreenCamera("c:\\屏幕抓屏", "jpg");
		//cam.snapShot();
		
		draw();
	}
}